package com.yami.shop.api.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.yami.shop.bean.app.dto.OrderShopDto;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.otherservice.OneLevelDistributionService;
import com.yami.shop.security.api.util.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/p/oneLevelDistribution")
public class OneLevelDistributionController {

    @Autowired
    private OneLevelDistributionService oneLevelDistributionService;

    /**
     * 查询团队长昨天的奖励金额
     * @return
     */
    @GetMapping("/getTeamLeaderBonus")
    public ServerResponseEntity<Map<String,Object>> getTeamLeaderBonus(){
        String userId = SecurityUtils.getUser().getUserId();

        //查询昨天
        DateTime yesterday = DateUtil.yesterday();
        //测试时查询指定日期
        //yesterday = DateUtil.parse("2024-08-22 16:49:24");

        Long teamPerformance = oneLevelDistributionService.getTeamPerformance(userId,yesterday);
        if(teamPerformance == null){
            teamPerformance = 0L;
        }

        String teamLeaderReward = oneLevelDistributionService.getValueByConfig("team_leader_reward");
        if(StringUtils.isBlank(teamLeaderReward) || new BigDecimal(teamLeaderReward).compareTo(BigDecimal.ZERO) <= 0){
            return ServerResponseEntity.showFailMsg("团队长奖励未配置");
        }

        //指定日期的奖励金额
        BigDecimal multiply = new BigDecimal(String.valueOf(teamPerformance)).multiply(new BigDecimal(teamLeaderReward));

        Map<String,Object> rmap=new HashMap<>();
        rmap.put("date",DateUtil.format(yesterday, "yyyy-MM-dd HH:mm:ss"));
        rmap.put("confirmReceiptNum",teamPerformance);
        rmap.put("rewardAmount",multiply);

        return ServerResponseEntity.success(rmap);
    }

    /**
     * 查看每天分红
     * @return
     */
    @GetMapping("/getDayBonus")
    public ServerResponseEntity<Map<String,Object>> getdayBonus(){
        DateTime yesterday = DateUtil.yesterday();
        //测试时查询指定日期
        yesterday = DateUtil.parse("2024-08-22 16:49:24");


        String dayBonus = oneLevelDistributionService.getValueByConfig("day_bonus");
        if(StringUtils.isBlank(dayBonus) || new BigDecimal(dayBonus).compareTo(BigDecimal.ZERO) <= 0){
            return ServerResponseEntity.showFailMsg("团队长奖励未配置");
        }

        //当日下单量
        Long dayBonusPlaceOrder = oneLevelDistributionService.getDayBonusPlaceOrder(yesterday);

        //当日收货量
        Long dayBonusTakeDelivery = oneLevelDistributionService.getDayBonusTakeDelivery(yesterday);

        //总分红金额
        BigDecimal multiply = new BigDecimal(String.valueOf(dayBonusTakeDelivery)).multiply(new BigDecimal(dayBonus));

        //当前可分红用户
        List<User> dayBonusUser = oneLevelDistributionService.getDayBonusUser();

        if(dayBonusUser.size() == 0){
            return ServerResponseEntity.showFailMsg("无可分红用户");
        }

        //每人可分红金额
        BigDecimal bigDecimal = multiply.divide(new BigDecimal(String.valueOf(dayBonusUser.size()))).setScale(4, RoundingMode.DOWN);

        Map<String,Object> rmap=new HashMap<>();
        rmap.put("eachMoney",dayBonus);//每份金额
        rmap.put("placeOrder",dayBonusPlaceOrder);//下单数量
        rmap.put("takeDelivery",dayBonusTakeDelivery);//收货数量
        rmap.put("totalAmount",multiply);//分红总金额
        rmap.put("dayBonusUserNum",dayBonusUser.size());//分红人数
        rmap.put("userAmount",bigDecimal);//每人分红金额
        rmap.put("date",DateUtil.format(yesterday, "yyyy-MM-dd"));//统计日期

        return ServerResponseEntity.success(rmap);
    }
}
